<?php
// 接口-采购申请接口
namespace interface\v1;


use verify;

class purchaseApplyClass extends index
{
	private const page    = 'purchaseApply';
	private const table   = 'purchase_apply';
	private const tableMx = 'purchase_apply_detail';
	private const title   = '采购申请';

	public function search()
	{
		$this->code         = 1;
		$this->resp['data'] = [
			'orderBy' => [
				'创建时间降序',
				'创建时间升序',
				'单据日期降序',
				'单据日期升序',
				'需求日期降序',
				'需求日期升序',
			],
			'search'  => [
				'type'       => [
					'key'   => 'type',
					'name'  => '单据类型',
					'array' => explode('，', para('purchaseType')),
				],
				'recordDate' => [
					'key'   => 'recordDate',
					'name'  => '单据日期',
					'array' => $this->timeOption,
				],
				'demandDate' => [
					'key'   => 'demandDate',
					'name'  => '需求日期',
					'array' => $this->timeOption,
				],
				'state'      => [
					'key'   => 'state',
					'name'  => '状态',
					'array' => [
						'草稿', '待确认', '已确认',
					],
				],
			],
		];
	}

	/**
	 * 列表
	 *
	 * @return void
	 */
	public function list()
	{
		$where = '';
		//获取查询结果
		$where .= search([
			"="    => [
				'coid'  => self::$coid,
				'type'  => $this->post('type'),
				'state' => $this->post('state'),
			],
			'like' => [
				'orderNo' => $this->post('orderNo'),
			],
		]);
		//单据日期
		if ($this->post('recordDate')) {
			$where .= self::miniTimeComparison($this->post('recordDate'), 'date', 'recordDate');
		}
		//需求日期
		if ($this->post('demandDate')) {
			$where .= self::miniTimeComparison($this->post('demandDate'), 'date', 'demandDate');
		}
		$orderBy      = $this->post('orderBy') ?: '创建时间降序';
		$orderByArray = $this->orderBy([
			"search"  => $orderBy,
			"default" => "创建时间降序",
			"array"   => [
				"time"         => ["asc" => "创建时间升序", "desc" => "创建时间降序", "site" => 1],
				"recordDate"   => ["asc" => "单据日期升序", "desc" => "单据日期降序", "site" => 2],
				"demandDate" => ["asc" => "需求日期升序", "desc" => "需求日期降序", "site" => 3],
			],
		]);
		$page = $this->page(["table" => self::table, "where" => $where, "order" => $orderByArray['orderBy'], ]);
		//数据分析
		$data['result'] = [];
		foreach ($page['array'] as $array) {
			$result           = find([
				'table'  => self::tableMx,
				'field'  => ' any_value(materialId) as materialId, IFNULL(sum(num), 0) as total ',
				'single' => " orderId = '$array[id]' ",
			]);
			$material         = query('material', " id = '$result[materialId]' ");
			$data['result'][] = [
				"id"        => $array['id'],
				'orderNo'   => $array['orderNo'],
				'type'      => $array['type'],
				'material'  => $material['name'] . '等' . $result['total'] . $material['unit'],
				"state"     => $array['state'],
				'staffName' => self::findStaff($array['stid']),
				'money'     => find(['table' => self::tableMx, 'where' => "orderId = '$array[id]'", 'sum' => "money"]),
				'time'      => self::formatDate($array['time'], 'Y/m/d'),
			];
		}
		$data['page'] = $page['para'];
		$data['orderBy'] = $orderBy;
		//返回
		$this->code         = 1;
		$this->resp['data'] = $data;
	}


	/**
	 * 选择物料
	 */
	public function material()
	{
		$materialId = $this->post('materialId') ? explode(',', $this->post('materialId')) : [];
		$where      = " 1=1 and .id not in ('" . implode("','", $materialId) . "')";
		$and        = " AND a.coid = '" . self::$coid . "' ";
		if ($this->post('wd')) {
			$and .= " AND (a.name like '%{$this->post('wd')}%' OR code like '%{$this->post('wd')}%') ";
		}
		$equal  = [
			'a.state' => '启用',
		];
		$where  .= search(["=" => $equal]) . $and;
		$orderBy = $this->orderBy([
			"search"  => $this->post('orderBy') ?: null,
			"default" => "创建时间降序",
			"array"   => [
				"a.time"           => ["asc" => "创建时间升序", "desc" => "创建时间降序", "site" => 2],
				"a.updateTime"     => ["asc" => "更新时间升序", "desc" => "更新时间降序", "site" => 3],
			],
		]);
		$page   = $this->page([
			"table"   => "material as a",
			"field"   => "a.*",
			"where"   => " $where ",
			'pageNum' => 50,
			"order" => $orderBy['orderBy']
		]);
		$result = [];
		//获取记录
		foreach ($page['array'] as $array) {
			$inventoryNum = inventoryBalanceClass::inventoryNum($array['materialId']);
			$result[]     = [
				'materialId'      => $array['id'],
				'name'            => $array['code'] . ' ' . $array['name'] . $array['model'],
				'model'           => $array['model'],
				'num'             => $inventoryNum,
				'unit'            => $array['unit'],
				'availableNum'    => $inventoryNum,
			];
		}
		$orderByArr         = [
			'创建时间降序',
			'创建时间升序',
			'更新时间降序',
			'更新时间升序',
		];
		$this->resp['data'] = [
			'result'  => $result,
			'page'    => $page['para'],
			'orderBy' => $orderByArr,
		];
		$this->code         = 1;
	}


	/**
	 * 编辑
	 */
	public function edit(): void
	{
		// 解析字符串
		$detail           = !empty($_POST['detail']) ? json_decode($_POST['detail'], true) : [];
		$id               = $this->post('id') ?: self::getId();
		$mxIdArr          = $this->post('mxId') ?: inStockClass::getArrVal('id', $detail);
		$materialIdArr = $this->post('materialId') ?: inStockClass::getArrVal('materialId', $detail);
		$numArr           = $this->post('num') ?: inStockClass::getArrVal('num', $detail);
		$priceArr         = $this->post('price') ?: inStockClass::getArrVal('price', $detail);
		$mxTextArr        = $this->post('mxText') ?: inStockClass::getArrVal('mxText', $detail);
		$rateArr          = $this->post('rate') ?: inStockClass::getArrVal('rate', $detail);

		$field      = [
			'type'       => $this->post('type'),
			'recordDate' => $this->post('recordDate'),
			'demandDate' => $this->post('demandDate'),
			'text'       => $this->post('text'),
		];
		$annotation = [
			'recordDate' => '单据日期',
			'demandDate' => '需求日期',
			'text'       => '备注',
		];
		$rules      = [
			'type'       => 'required',
			'recordDate' => 'required|date',
			'demandDate' => 'required|date',
			// 'text'           => 'required',
		];
		// 校验是否符合规则
		$vt     = new verify();
		$result = $vt->setAliases($annotation)->validate($field, $rules)->isOk();
		if ($result === false) {
			$this->resp['warn'] = $vt->getErrs()[0]; // 多个规则提示最前面一条
			goto resp;
		}
		$where = " coid = '" . self::$coid . "' AND id = '$id' ";
		$info  = query(self::table, $where);
		//判断
		if (!$this->power(self::page, "edit")) {
			$this->resp['warn'] = "你没有编辑" . self::title . "的权限";
		} elseif (self::is_null_array($mxIdArr)) {
			$this->resp['warn'] = '请选择产品明细';
		} elseif (self::is_null_array($materialIdArr)) {
			$this->resp['warn'] = '请选择产品明细';
		} elseif (self::is_null_array($numArr)) {
			$this->resp['warn'] = '数量请填写完整';
		} elseif (self::is_null_array($priceArr)) {
			$this->resp['warn'] = '价格请填写完整';
		} elseif (self::is_null_array($rateArr)) {
			$this->resp['warn'] = '请输入税率';
		} elseif (!empty($info['id']) && $info['state'] == '已通过') {
			$this->resp['warn'] = '此采购申请单已通过，不能修改';
		} else {
			$this->startTrans();
			$isPass = true;//事务通过标志
			if (!$info['id']) {
				$orderNo            = self::generateCode(self::page,[
                    'prefix' => 'CGSQ' . date('Ymd'),
                    'table'  => self::table,
                    'field'  => 'orderNo',
                    'where'  => '',
                ],$field);
				$field              = array_merge($field, [
					'id'      => $id,
					'orderNo' => $orderNo,
					'state'   => '草稿',
					'coid'    => self::$coid,
					'stid'    => $this->control['stid'],
				]);
				$this->resp['warn'] = static::insert(["table" => self::table, "field" => $field,]);
			} else {
				$this->resp['warn'] = static::update([
					"table" => self::table,
					"field" => $field,
					"where" => $where,
				]);
			}
			//添加或更新采购申请单明细
			if (is_array($mxIdArr)) {
				foreach ($mxIdArr as $k => $nid) {
					$mx          = query(self::tableMx, "id = '$nid'");
					if (!empty($mx['id'])) {
						//更新
						$warn = static::update([
							"table" => self::tableMx,
							"field" => [
								'materialId'    => $materialIdArr[$k],
								'num'           => $numArr[$k],
								'price'         => $priceArr[$k],
								'coid'          => self::$coid,
								'rate'          => $rateArr[$k],
								'money'         => round($numArr[$k] * $priceArr[$k]*(1+$rateArr[$k]), 2),
								'text'          => $mxTextArr[$k],
							],
							"where" => " id = '$nid'",
						]);
					} else {
						//新增
						$warn = static::insert([
							"table" => self::tableMx,
							"field" => [
								'id'            => $nid,
								'coid'          => self::$coid,
								'orderId'       => $id,
								'materialId'    => $materialIdArr[$k],
								'num'           => $numArr[$k],
								'price'         => $priceArr[$k],
								'rate'          => $rateArr[$k],
								'money'         => round($numArr[$k] * $priceArr[$k]*(1+$rateArr[$k]), 2),
								'text'          => $mxTextArr[$k],
							],
						]);
					}
					if (!in_array($warn, ['新增成功', '更新成功'])) {
						$isPass             = false;
						$this->resp['warn'] = $warn;
						break;
					}
				}
			}
			if (in_array($this->resp['warn'], ["新增成功", "更新成功"]) && $isPass) {
				$this->commitTrans();
				// 更新金额
				delete(['table' => self::tableMx, 'where' => " id not in ('" . implode("','", $mxIdArr) . "') and orderId = '$id'"]);
				//添加日志
				$type = mb_substr($this->resp['warn'], 0, 2, 'utf-8');
				$text = $this->control['name'] . $type . "了" . self::title . " id：【" . $id . '】';
				$this->logText(["type" => self::page, "text" => $text]);
				//返回
				$this->code         = 1;
				$this->resp['data'] = $id;
			} else {
				$this->rollback();
			}
		}

		resp:
	}

	/**
	 * 提交
	 */
	public function commit()
	{
		$id    = $this->post('id');
		$info  = query(self::table, " coid = '" . self::$coid . "' AND id = '$id'");
		$field = [
			'state' => '审核中',
		];
		if (!$this->power(self::page, 'edit')) {
			$this->resp['warn'] = '你还没有编辑权限';
		} elseif (empty($info['id'])) {
			$this->resp['warn'] = '未找到此采购申请单';
		} elseif ($info['state'] != '草稿') {
			$this->resp['warn'] = '此采购申请单不是草稿状态，不能提交';
		} else {
			$this->resp['warn'] = update(array(
				"table" => self::table,
				"field" => $field,
				"where" => "id = '$info[id]'",
			));
			if (in_array($this->resp['warn'], ["新增成功", "更新成功"])) {
				auditingFollow([
					"type"   => "采购申请单",
					"typeId" => $info['id'],
					"text"   => "【{$this->control['name']}】提交",
				]);
				//添加日志
				$type = mb_substr($this->resp['warn'], 0, 2, 'utf-8');
				$text = $this->control['name'] . $type . "了" . self::title . "id：【" . $id . '】';
				$this->logText(["type" => self::page, "text" => $text]);
				//返回
				$this->code = 1;
			}
		}
		resp:
	}

	/**
	 * 确认
	 */
	public function confirmEdit()
	{
		$id    = $this->post('id');
		$info  = query(self::table, " coid = '" . self::$coid . "' AND id = '$id'");
		$field = [
			'state'       => '已通过',
			'checkId'     => self::$stid,
			'confirmTime' => $this->time,
		];
		if (!$this->power(self::page, 'confirm')) {
			$this->resp['warn'] = '你还没有确认权限';
		} elseif (empty($info['id'])) {
			$this->resp['warn'] = '未找到此采购申请单';
		} elseif ($info['state'] != '审核中') {
			$this->resp['warn'] = '此申请不是审核中状态，不能确认';
		} else {
			$this->resp['warn'] = update([
				"table" => self::table,
				"field" => $field,
				"where" => "id = '$info[id]'",
			]);
			if (in_array($this->resp['warn'], ["新增成功", "更新成功"])) {
				auditingFollow(array(
					"type"   => "采购申请单",
					"typeId" => $info['id'],
					"text"   => "【{$this->control['name']}】确认",
				));
				//添加日志
				$type = mb_substr($this->resp['warn'], 0, 2, 'utf-8');
				$text = $this->control['name'] . $type . "了" . self::title . "id：【" . $id . '】';
				$this->logText(["type" => self::page, "text" => $text]);
				//返回
				$this->code = 1;
			}
		}

		resp:
	}

	/**
	 * 删除
	 *
	 * @return void
	 */
	public function delete(): void
	{
		$array = $this->post('listId') ?? $this->post('id');
		if (!$this->power(self::page, "del")) {
			$this->resp['warn'] = "你没有删除的权限";
		} elseif (empty($array)) {
			$this->resp['warn'] = "你一个数据都没有选择呢";
		} else {
			$text  = "";
			$x     = 0;
			$array = is_array($array) ? $array : [$array];
			foreach ($array as $id) {
				$info = query(self::table, " id = '$id' ");
				if ($info['state'] == '已通过') {
					$warn = "删除失败：已通过，暂不能删除";
				} else {
					//最后删除基本参数
					delete(["table" => self::table, "where" => " id = '$id' "]);
					delete(["table" => self::tableMx, "where" => " orderId = '$id' "]);
					delete(["table" => 'auditing_follow', "where" => " typeId = '$id' "]);
					//返回
					$warn = "删除成功";
					$x++;
				}
				$text .= self::title . "ID【" . $info['id'] . "】" . $warn . "。<br>";
			}
			//添加日志
			$text = $this->control['name'] . "删除了" . self::title . "，结果如下：<br>" . $text;
			$this->logText(array("type" => self::page, "text" => $text));
			$this->resp['warn'] = "删除了" . $x . "个" . self::title . "，详情见系统日志。";
			$this->code         = 1;
		}
	}

	/**
	 * 详情
	 *
	 * @return void
	 */
	public function detail()
	{
		$id   = $this->post('id');
		$info = query(self::table, "id = '" . $id . "'");
		// if (!in_array(self::$stid, [$info['checkId'], $info['stid']])) {
		// 	$this->resp['warn'] = '这不是您的' . self::title;
		// } else
		if (empty($id)) {
			$this->resp['warn'] = "ID为空";
		} elseif (empty($info['id'])) {
			$this->resp['warn'] = "未找到此" . self::title;
		} else {
			$audit  = find([
				'table' => 'auditing_follow',
				'where' => " typeId = '$info[id]' order by time desc",
			]);
			$result = find([
				'table' => self::tableMx,
				'where' => " orderId = '$info[id]' ",
			]);
			$detail = [];
			$totalMoney = 0;
			foreach ($result['array'] as $value) {
				$material    = query('material', " id = '$value[materialId]' ");
				$detail[]    = [
					'id'            => $value['id'],
					'code'          => $material['code'],
					'name'          => $material['name'],
					'model'         => $material['model'],
					'unit'          => $material['unit'],
					'price'         => $value['price'],
					'num'           => $value['num'],
					'rate'          => $value['rate'],
					'text'          => $value['text'],
					'materialId'    => $value['materialId'],
					'rateMoney'     => $value['num'] * $value['price'] * $value['rate'],
					'total'         => $value['num'] * $value['price'] * (1 + $value['rate']),
				];
				$totalMoney += $value['num'] * $value['price'] * (1 + $value['rate']);
			}
			$data['result'] = [
				'info'        => [
					"id"         => $info['id'],
					'staffName'  => self::findStaff($info['stid']),
					'orderNo'    => $info['orderNo'],
					'recordDate' => $info['recordDate'],
					'demandDate' => $info['demandDate'],
					'type'       => $info['type'],
					"state"      => $info['state'],
					'text'       => $info['text'],
					'date'       => self::formatDate($info['time'], 'm/d'),
				],
				'detail'      => $detail,
				'auditRecord' => $audit['array'],
				'total'       => [
					'num'   => count($detail),
					'money' => $totalMoney,
				],
			];
			//返回
			$this->code         = 1;
			$this->resp['data'] = $data;
		}
	}

}



